/*
 * Copyright 2010-2020 Alfresco Software, Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.activiti.engine.impl.bpmn.behavior;

import org.activiti.bpmn.model.BoundaryEvent;
import org.activiti.bpmn.model.ExtensionAttribute;
import org.activiti.bpmn.model.TimerEventDefinition;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.debug.Contants;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.impl.asyncexecutor.AsyncExecutor;
import org.activiti.engine.impl.asyncexecutor.JobManager;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.jobexecutor.AsyncExecutionJobNotification;
import org.activiti.engine.impl.jobexecutor.TimerEventHandler;
import org.activiti.engine.impl.jobexecutor.TriggerTimerEventJobHandler;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionJobEntityImpl;
import org.activiti.engine.impl.persistence.entity.JobEntity;
import org.activiti.engine.impl.persistence.entity.TimerJobEntity;

/**
 */
public class BoundaryTimerEventActivityBehavior extends BoundaryEventActivityBehavior {

	private static final long serialVersionUID = 1L;

	protected TimerEventDefinition timerEventDefinition;

	public BoundaryTimerEventActivityBehavior(TimerEventDefinition timerEventDefinition, boolean interrupting) {
		super(interrupting);
		this.timerEventDefinition = timerEventDefinition;
	}

	@Override
	public void execute(DelegateExecution execution) {

		ExecutionEntity executionEntity = (ExecutionEntity) execution;
		if (!(execution.getCurrentFlowElement() instanceof BoundaryEvent)) {
			throw new ActivitiException("Programmatic error: " + this.getClass() + " should not be used for anything else than a boundary event");
		}

		CommandContext commandContext = Context.getCommandContext();
		JobManager jobManager = commandContext.getJobManager();
		TimerJobEntity timerJob = jobManager.createTimerJob(timerEventDefinition, interrupting, executionEntity,
				TriggerTimerEventJobHandler.TYPE,
				TimerEventHandler.createConfiguration(execution.getCurrentActivityId(), timerEventDefinition.getEndDate(), timerEventDefinition.getCalendarName()));
		if(JobEntity.JOB_TYPE_IMME.equals(timerJob.getJobType())) {
			BoundaryEvent event = (BoundaryEvent)execution.getCurrentFlowElement();
			ExtensionAttribute attr = new ExtensionAttribute(Contants.BOUNDARYTIMEREVENT_TYPE);
			attr.setValue(JobEntity.JOB_TYPE_IMME);
			event.addAttribute(attr);
			ExtensionAttribute attr2 = new ExtensionAttribute(Contants.BOUNDARYTIMEREVENT_EXECUTION_ID);
			attr2.setValue(executionEntity.getId());
			event.addAttribute(attr2);

			// 立即执行任务
			ExecutionJobEntityImpl job = new ExecutionJobEntityImpl(executionEntity, false, true);
			AsyncExecutionJobNotification jobAddedNotification = new AsyncExecutionJobNotification(job, getAsyncExecutor());
			jobAddedNotification.serviceExecute(commandContext);
		} else {
			if (timerJob != null) {
				jobManager.scheduleTimerJob(timerJob);
			}
		}
	}

	private AsyncExecutor getAsyncExecutor() {
		return Context.getProcessEngineConfiguration().getAsyncExecutor();
	}

}
